<?php

namespace Weapp\Product;

use Weapp\Basic\Basic;
use Weapp\Basic\MyCurlFile;

/**
 * 标准版交易组件及开放接口，接入商品前必需接口
 * Class Common
 * @url https://developers.weixin.qq.com/miniprogram/dev/platform-capabilities/business-capabilities/ministore/minishopopencomponent/API/cat/get_cat_list.html
 * @package Weapp/Product
 */
class Common extends Basic
{
    /**
     * 获取店铺信息
     * @param array $data
     * @return array
     * @throws \Weapp\Exceptions\InvalidResponseException
     * @throws \Weapp\Exceptions\LocalCacheException
     */
    public function get_info($data = [])
    {
        $url = 'product/store/get_info';
        $this->registerApi($url, __FUNCTION__, func_get_args());
        return $this->callPostApi($url, $data, true);
    }

    /**
     * 获取类目详情
     * 多次调用该接口分别拿到一，二，三级类目，如果某个二级类目下的三级类目是空，则说明这个二级类目下的所有三级类目都需要到shop.weixin.qq.com申请资质
     * @param array $data
     * @return array
     * @throws \Weapp\Exceptions\InvalidResponseException
     * @throws \Weapp\Exceptions\LocalCacheException
     */
    public function category($data = [])
    {
        $url = 'product/category/get';
        $this->registerApi($url, __FUNCTION__, func_get_args());
        return $this->callPostApi($url, $data, true);
    }
    /**
     * 获取品牌列表
     * 
     * @param array $data
     * @return array
     * @throws \Weapp\Exceptions\InvalidResponseException
     * @throws \Weapp\Exceptions\LocalCacheException
     */
    public function brand($data = [])
    {
        $url = 'product/brand/get';
        $this->registerApi($url, __FUNCTION__, func_get_args());
        return $this->callPostApi($url, $data, true);
    }

    /**
     * 获取运费模板
     * 
     * @param array $data
     * @return array
     * @throws \Weapp\Exceptions\InvalidResponseException
     * @throws \Weapp\Exceptions\LocalCacheException
     */
    public function get_freight_template($data = [])
    {
        $url = 'product/delivery/get_freight_template';
        $this->registerApi($url, __FUNCTION__, func_get_args());
        return $this->callPostApi($url, $data, true);
    }
    /**
     * 获取店铺的商品分类
     * 
     * @param array $data
     * @return array
     * @throws \Weapp\Exceptions\InvalidResponseException
     * @throws \Weapp\Exceptions\LocalCacheException
     */
    public function get_shopcat($data = [])
    {
        $url = 'product/store/get_shopcat';
        $this->registerApi($url, __FUNCTION__, func_get_args());
        return $this->callPostApi($url, $data, true);
    }

    /**
     * 上传图片（大小限制2MB）
     * 
     * @param array $data
     * @return array
     * @throws \Weapp\Exceptions\InvalidResponseException
     * @throws \Weapp\Exceptions\LocalCacheException
     */
    public function uploadImg($data)
    {
        $url = 'product/img/upload';
        if (isset($data['img_url'])) {
            $myFile = new MyCurlFile($data['img_url']);
            $data['media'] = $myFile->get();
        }
        $this->registerApi($url, __FUNCTION__, func_get_args());
        return $this->callPostApi($url, $data, false);
    }
}
